package krakow.ae.knp.wikiboard.gui.tools.markers;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.Color;
import krakow.ae.knp.wikiboard.gui.Board;

public class EllipseMarker extends FigureMarker {
    
    private Point2D startPoint;
    private Ellipse2D ellipse;
    private static int instance_counter = 1;
    
    
    /** Creates a new instance of EllipseMarker */
    public EllipseMarker()     {
        super( "Ellipse" + instance_counter++ );
        ellipse = null;
        startPoint = null;
    }
    
    public void drawFigure(Graphics2D g, boolean dragged) {
        g.setXORMode(Board.bgColor);
        if(!dragged)
            g.setPaintMode();
        g.setColor(Board.fgColor);
        g.draw(ellipse);
    }
    
    private java.awt.geom.Ellipse2D.Double newEllipse2D(double endX,
            double endY) {
        double ex;
        double ey;
        double width;
        double height;
        double x = startPoint.getX();
        double y = startPoint.getY();
        
        if (x < endX)         {
            ex = x;
            width = endX - x;
        } else        {
            ex = endX;
            width = x - endX;
        }
        
        if (y < endY)        {
            ey = y;
            height = endY - y;
        } else        {
            ey = endY;
            height = y - endY;
        }
        return new java.awt.geom.Ellipse2D.Double(ex, ey, width, height);
    }
    
    
    public void mouseDragged(int x, int y, Graphics2D g)    {
        g.setXORMode(Board.bgColor);
        g.setColor(Board.fgColor);
        g.draw( ellipse ); // ???
        ellipse = newEllipse2D(x, y);
        g.draw( ellipse );
    }
    
    public void mousePressed(int x, int y, Graphics2D g)     {
        startPoint = new Point2D.Double(x, y);
        ellipse = newEllipse2D(x, y);
    }
    
    public boolean mouseReleased(int x, int y, Graphics2D g)    {
        g.setXORMode(Board.bgColor);
        g.setColor(Board.fgColor);
        g.draw(ellipse);
        ellipse = newEllipse2D(x, y);
        g.setPaintMode();
        g.draw( ellipse );
        return ( (ellipse.getWidth() > 0.0) || (ellipse.getHeight() > 0.0) );
    }
    
    public boolean contains(Point2D p)    {
        return ellipse.intersects( p.getX() - 1.0, p.getY() - 1.0, 3.0, 3.0);
    }
    
    public void move(double x, double y)    {
        ellipse = new Ellipse2D.Double( ellipse.getX() + x, ellipse.getY() + y,
                ellipse.getWidth(), ellipse.getHeight() );
    }
}
